﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSGeo.MapGuide;
using IGIS;
using System.Web.Services;
using System.Xml;
using System.Text;
using System.IO;
using System.Linq;

using System.Net;
using System.Diagnostics;
using System.Reflection;
using System.Configuration;
using System.Collections.Specialized;
using System.Data;
using IgisApi;
using System.Data.Entity.Infrastructure;


namespace IGIS.Extensions
{
    public partial class FindPolygon : utilityfunctions 
    {
        public findlocationfunctions jsonlf = new findlocationfunctions();
       public  String mgSessionId;// = GetRequestParameters()["SESSION"];
      public   String mgMap;// = GetRequestParameters()["MAPNAME"];
        bool showPreviousResults = false;
       public  MgSiteConnection siteConnection = null;
       public  String dblKanam;
      public  decimal LatInput = 0;
      public  decimal LonInput = 0;
        
       findlocationfunctions lf = new findlocationfunctions();
       public bool success = false;
       public string mgPolygonID = string.Empty;
       //GeocodeAddress addr = null;
        public TBL_Project _currentPolygon ;
       
        protected void Page_Load(object sender, EventArgs e)
        {             
                try
                {
                    mgPolygonID = GetRequestParameters()["ID"];
                      
                    GetPolygonDetails (Convert.ToDecimal(mgPolygonID));
                    FillProjectDetailsUI();
                        if (!Page.IsPostBack)
                        {
                            ClearSessions();
                            FillDropDowns();
                        }
                        
                    
                   
                        

                }
                catch (MgException mge)
                {
                   // Response.Write(mge.GetExceptionMessage());
                    //Response.Write(mge.GetDetails());
                }


        }
        // Create a temporary Layer to display geocode results.

        private void GetPolygonDetails(decimal id)
        {
            using (HetzEntities db = new HetzEntities())
            {

                _currentPolygon = db.TBL_Project.FirstOrDefault(x => x.PolygonID == id);
                        //where p.PolygonID == id ;
            }

        
        }
        private void FillProjectDetailsUI()
        {

            if (null != _currentPolygon)
            {

                StartDate.Value = _currentPolygon.StartDate ==null ? "" : _currentPolygon.StartDate.ToString();
                ProjectName.Value = _currentPolygon.ProjectName == null ? "" : _currentPolygon.ProjectName;
                ProjectNum.Value = _currentPolygon.ProjectNum == null ? "" : _currentPolygon.ProjectNum;
                drpSiteTypes.ClearSelection();
                if (null != _currentPolygon.SiteTypeID)
                {
                    drpSiteTypes.SelectedValue =_currentPolygon.SiteTypeID.ToString();
                }
                if (null != _currentPolygon.SponsorID)
                {
                    DrpSponsor.SelectedValue = _currentPolygon.SponsorID.ToString();
                }
                if (null != _currentPolygon.OrdererID)
                {
                    DrpOrderer.SelectedValue = _currentPolygon.OrdererID.ToString();
                }

                if (null != _currentPolygon.PriorityID)
                {
                    DrpPriority.SelectedValue = _currentPolygon.PriorityID.ToString();
                }
                if (null != _currentPolygon.PerfStatusID)
                {
                    DrpPrefStatus.SelectedValue = _currentPolygon.PerfStatusID.ToString();
                }
                if (null != _currentPolygon.CoordinatorID)
                {
                    DrpCoordinator.SelectedValue = _currentPolygon.CoordinatorID.ToString();
                }
                Comment.Value = _currentPolygon.Comment == null ? "" : _currentPolygon.Comment;
                ProjectDesc.Value = _currentPolygon.ProjectDesc == null ? "" : _currentPolygon.ProjectDesc;
                ContactOrderer.Value = _currentPolygon.ContactOrderer == null ? "" : _currentPolygon.ContactOrderer;
                ContactSponsor.Value = _currentPolygon.ContactSponsor == null ? "" : _currentPolygon.ContactSponsor;
            }
        }
        private void FillDropDowns()
        {

            using (HetzEntities db=new HetzEntities())
            {
              
                var query = from p in db.LUT_SiteType
                            orderby p.SiteTypeID ascending
                            select p;
                drpSiteTypes.DataSource = query.ToList<LUT_SiteType>();

                drpSiteTypes.DataTextField = "Value";
                drpSiteTypes.DataValueField = "SiteTypeID";
                drpSiteTypes.DataBind();

                var queryPriority =   from p in db.LUT_Priority 
                                        orderby p.PriorityID ascending
                                        select p ;
                DrpPriority.DataSource = queryPriority.ToList<LUT_Priority>();
                DrpPriority.DataTextField = "Value";
                DrpPriority.DataValueField = "PriorityID";
                DrpPriority.DataBind();

                var queryPrefStatus = from p in db.LUT_PerfStatus 
                                        orderby p.PrefStatusID ascending
                                        select p ;
                DrpPrefStatus.DataSource = queryPrefStatus.ToList<LUT_PerfStatus>();
                DrpPrefStatus.DataTextField = "Value";
                DrpPrefStatus.DataValueField = "PrefStatusID";
                DrpPrefStatus.DataBind();


                var queryCoordinator = from p in db.LUT_Coordinator
                                      orderby p.CoordinatorID ascending
                                      select p;
                DrpCoordinator.DataSource = queryCoordinator.ToList<LUT_Coordinator>();
                DrpCoordinator.DataTextField = "Value";
                DrpCoordinator.DataValueField = "CoordinatorID";
                DrpCoordinator.DataBind();

                var queryOrderer = from p in db.LUT_Orderer
                                       orderby p.SponsorID ascending
                                       select p;
                DrpOrderer.DataSource = queryOrderer.ToList<LUT_Orderer>();
                DrpOrderer.DataTextField = "SponsorName";
                DrpOrderer.DataValueField = "SponsorID";
                DrpOrderer.DataBind();


                var querySponsor = from p in db.LUT_sponser
                                   orderby p.SponsorID ascending
                                   select p;
                DrpSponsor.DataSource = querySponsor.ToList<LUT_sponser>();
                DrpSponsor.DataTextField = "SponsorName";
                DrpSponsor.DataValueField = "SponsorID";
                DrpSponsor.DataBind();
                //db.LU
              //  drpSiteTypes.DataSource = 
                
            }
        }


        
        protected void Button2_Click(object sender, EventArgs e)
        {
           
            
        }
      
        private void ClearSessions()
        {
         try
            {
                InitializeWebTier();

                MgUserInformation userInfo = new MgUserInformation(mgSessionId);
                MgSiteConnection siteConnection = new MgSiteConnection();
                siteConnection.Open(userInfo);
                MgResourceService resourceService = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
                MgFeatureService featureService = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);

                MgMap map = new MgMap();
                map.Open(resourceService, mgMap);


                MgLayerCollection layerCol = map.GetLayers();



                string layerName = "LocationMarker";

MgLayer layerToRemove = null;
foreach(MgLayer layer in layerCol)
{
      if(layer.Name == layerName)
      {
            layerToRemove = layer;
            break;
      }
}

if(layerToRemove != null)
{
      layerCol.Remove(layerToRemove);
}




                map.Save(resourceService);
                
                targetsnewTitle.InnerHtml = "";
                targetsnew.InnerHtml = "";
                targetsold.InnerHtml="";
                targetsoldTitle.InnerHtml = "";

                Response.Write("<script>try{ parent.parent.mapFrame.Refresh();   parent.parent.mapFrame.ZoomToSelection();}catch(e){}</script>");
                /* success = true;*/
            
            }
            catch (Exception exx)
            { 
            
            }

        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (_currentPolygon != null)
            {
                using (HetzEntities db =new HetzEntities())
                {

                    var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;
                    manager.ChangeObjectState(_currentPolygon, EntityState.Modified);
                
                    db.SaveChanges();
                    
                }
            }
        }
   


       
       
    }
}